﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Library.Facade.SQLServer;
using System.Configuration;
using Web.Library.DomainObject.SQLServer;
using System.Text;

using Web.Support;
public partial class _default : BasePage
{
    private SysApplicationFacade Facade = new SysApplicationFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
    private SysApplicationGroupsFacade FacadeApp = new SysApplicationGroupsFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            


            
        if (!IsPostBack)
        {
            //AuthInfo info = new AuthInfo().UserInfo();

            List<SysApplicationGroups> listGroupApp = new List<SysApplicationGroups>();
            listGroupApp = FacadeApp.GetList();
            ddlGroup.Items.Clear();

            string[] groupApp = base.GroupAppID.Split(',');
            List<SysApplicationGroups> listFilter = new List<SysApplicationGroups>();
            foreach (string data in groupApp)
            {
                //var query = listGroupApp.Where(x => x.AppGroupID == Convert.ToInt32(data));

                SysApplicationGroups objGroup = FacadeApp.GetById(Convert.ToInt32(data));
                listFilter.Add(objGroup);
            }

            listFilter.ForEach(delegate(SysApplicationGroups groupapp)
            {
                ddlGroup.Items.Add(new ListItem(groupapp.AppGroupName, groupapp.AppGroupID.ToString()));
            });
            if (groupApp.Length > 1)
                ddlGroup.Items.Insert(0, new ListItem("--All---", "0"));

            LoadApp(base.GroupAppID);

     
        }

        }   

       catch (Exception ex)
            {
                  Logger.Debug("IsPostBack on Default.aspx",ex);
            }

    }

    private void LoadApp(string groupApp)
    {
        List<SysApplication> list = Facade.GetListByGroupAppID(groupApp);
        StringBuilder sb = new StringBuilder();

        foreach (SysApplication data in list)
        {
            sb.AppendLine("<div class=" + "\"" + "grid-block fade" + "\"" + ">");
            sb.AppendLine("<div class=" + "\"" + "caption" + "\"" + ">");
            sb.AppendLine("<h3>");
            sb.AppendLine(data.TitleApp + "</h3>");
            sb.AppendLine("<p>");
            //sb.AppendLine("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor");
            //sb.AppendLine("incididunt ut labore et dolore magna aliqua.</p>");
            sb.AppendLine(data.DescApp);
            sb.AppendLine("</p>");
            sb.AppendLine("<p>");
            sb.AppendLine("<a target='_blank' href=" + "\"" + data.UrlApp + "\"" + " class=" + "\"" + "learn-more" + "\"" + ">Browse</a></p>");
            sb.AppendLine("</div>");
            sb.AppendLine("<img width='300px' height=200px' src=" + "\"" + data.ImageApp + "\"" + " />");
            sb.AppendLine("<h4>");
            sb.AppendLine("<a target='_blank' href=" + "\"" + data.UrlApp + "\"" + " >" + data.TitleApp + "</a>");
            sb.AppendLine("</h4>");
            sb.AppendLine("</div>");
            // sb.AppendLine("<br />");
        }

        ltr.Text = sb.ToString();
    }
    protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
        string groupAppID = string.Empty;


        if (ddlGroup.Items.Count > 1)
        {
            if (ddlGroup.SelectedIndex == 0)
            {
                foreach (ListItem data in ddlGroup.Items)
                {
                    groupAppID += data.Value + ",";
                }

                LoadApp(groupAppID.Substring(0, groupAppID.Length - 1));
            }
            else
            {
                LoadApp(ddlGroup.SelectedItem.Value);
            }
        }
        else
        {
            LoadApp(ddlGroup.SelectedItem.Value);
        }
    }
}